﻿using System;

namespace Liphofra.Mvvm.Commands
{
    /// <summary>
    /// A convenience class of a relay command that takes no parameters.
    /// </summary>
    public class RelayCommand : RelayCommand<object>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand"/> class.
        /// </summary>
        /// <param name="commandAction">The action to invoke when the command is executed.</param>
        public RelayCommand(Action commandAction)
            : base(o => commandAction())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand"/> class.
        /// </summary>
        /// <param name="commandAction">The action to invoke when the command is executed.</param>
        /// <param name="canExecuteFunc">The function to use to determine whether the command can be executed.</param>
        public RelayCommand(Action commandAction, Func<bool> canExecuteFunc)
            : base(
                o => commandAction(), 
                o =>
                    {
                        if (canExecuteFunc != null)
                        {
                            return canExecuteFunc();
                        }
                        else
                        {
                            return true;
                        }
                    })
        {
        }
    }
}